package com.lyc.easyar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 画布：作画
 */
public class DrawBitmapUtil {
    public static int drawTop = 50;
    public static Bitmap drawBitmap(Context context) {
        // 创建一个你需要尺寸的Bitmap
        Bitmap mBitmap = Bitmap.createBitmap(1080, 1920, Config.ARGB_8888);
        // 用这个Bitmap生成一个Canvas,然后canvas就会把内容绘制到上面这个bitmap中
        Canvas mCanvas = new Canvas(mBitmap);
        // 画笔－－绘图
        Paint mPicturePaint = new Paint();
        // 绘制图片
        Bitmap bmScan = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.scan_shape);
        // 绘制图片－－保证其在水平方向居中
        mCanvas.drawBitmap(
                bmScan,
                (1080 - bmScan.getWidth())/2f,
                drawTop,
                mPicturePaint
        );
        drawTop += 15;
        if(drawTop >= 1920 - 50){
            drawTop = 50;
        }

        //四个角
        Paint linePoint = new Paint();
        linePoint.setStrokeWidth(10f);
        linePoint.setColor(Color.parseColor("#3D72FF"));
        mCanvas.drawLine(10f,10f,10f,60f, linePoint);
        mCanvas.drawLine(10f,10f,60f,10f, linePoint);

        mCanvas.drawLine(1070f,0f,1020f,0f, linePoint);
        mCanvas.drawLine(1070f,0f,1070f,50f, linePoint);

        mCanvas.drawLine(10f,1910f,10f,1860f, linePoint);
        mCanvas.drawLine(10f,1910f,60f,1910f, linePoint);

        mCanvas.drawLine(1070f,1910f,1020f,1910f, linePoint);
        mCanvas.drawLine(1070f,1910f,1070f,1860f, linePoint);

        // 画笔－－写字
//        Paint mTextPaint = new Paint();
//        mTextPaint.setColor(Color.WHITE);// 颜色
//        mTextPaint.setTextSize(30.0f);// 设置字体大小
//        for(int i=0;i<10;i++){
//            drawText(mCanvas, "测试数据第"+i+"行", mTextPaint, 50f, 60f+i*90f);
//        }

        mCanvas.save();
        mCanvas.restore();
        return mBitmap;
    }

    private static void drawText(Canvas canvas, String text, Paint paint, float x, float y){
        //计算字符的物理宽度
        //float textWidth = paint.measureText(text, 0, text.length());
        canvas.drawText(text, x, y, paint);// 绘制文字
    }

    public static void saveBitmapToFile(String filePath, Bitmap mBitmap){
        // 保存绘图为本地图片
        File file = new File(filePath);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            mBitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
